<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\service\BoxStatisticsAdGameTrackService;
use app\admin\service\BoxStatisticsAdLtvService;
use app\admin\service\BoxStatisticsAdOverviewService;
use app\admin\service\BoxStatisticsAdRemainService;
use app\admin\service\BoxStatisticsAdTrackService;
use app\common\controller\Export;
use Exception;
use think\Db;

class BoxStatisticsAd extends Backend
{
    /**
     * 投放渠道
     */
    public function getAdMediaList()
    {
        $mediaList = Db::name('box_ad_media')->field(['id', 'name'])->where(['status' => 1, 'mark' => 0])->order("id DESC")->select();

        return $this->jsonReturn(message("操作成功", true, $mediaList));
    }

    /**
     * @Notes:广告投放统计
     * @Interface overviewList
     */
    public function adOverviewList()
    {
        $this->service = new BoxStatisticsAdOverviewService();
        $result        = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * @Notes:导出广告投放统计
     * @Interface overviewExport
     */
    public function adOverviewExport()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        $Export = new Export();
        $Export->adOverviewExport($map);
    }

    /**
     * @Notes:下载广告投放统计模板
     * @Interface overviewExport
     */
    public function adOverviewDownloadModel()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        $Export = new Export();
        $Export->adOverviewDownloadModel($map);
    }


    /**
     * @Notes: 上传广告投放统计模板
     * @Interface overviewExport
     */
    public function adOverviewDownloadUpload()
    {
        $this->service = new BoxStatisticsAdOverviewService();
        $result        = $this->service->adOverviewDownloadUpload();
        return $this->jsonReturn($result);

    }

    /**
     * @Notes:广告投放留存
     * @Interface overviewList
     */
    public function adRemainList()
    {
        $this->service = new BoxStatisticsAdRemainService();
        $result        = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * @Notes:导出广告投放留存
     * @Interface adRemainExport
     */
    public function adRemainExport()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }
        $Export = new Export();
        $Export->adRemainExport($map);
    }

    /**
     * @Notes:广告投放LTV
     * @Interface overviewList
     */
    public function adLtvList()
    {
        $this->service = new BoxStatisticsAdLtvService();
        $result        = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * @Notes:导出广告投放LTV
     * @Interface adLtvExport
     */
    public function adLtvExport()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }
        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }
        $Export = new Export();
        $Export->adLtvExport($map);
    }

    /**
     * @Notes:盒子投放效果追踪
     * @Interface overviewList
     */
    public function adTrackList()
    {
        $this->service = new BoxStatisticsAdTrackService();
        $result        = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * @Notes:导出盒子投放效果追踪
     * @Interface adTrackExport
     */
    public function adTrackExport()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        $Export = new Export();
        $Export->adTrackExport($map);
    }

    /**
     * @Notes:盒子投放游戏数据总览
     * @Interface overviewList
     */
    public function adGameTrackList()
    {
        $this->service = new BoxStatisticsAdGameTrackService();
        $result        = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * @Notes:导出盒子投放游戏数据总览
     * @Interface adGameTrackExport
     */
    public function adGameTrackExport()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        if (isset($param['game_id']) && !empty($param['game_id'])) {
            $map[] = ['box_game_id', '=', $param['game_id']];
        }
        $Export = new Export();
        $Export->adGameTrackExport($param, $map);
    }

}


